package com.quectel.core.module.notice.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.core.NoticeConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.notice.dao.NoticeDao;
import com.quectel.core.module.notice.dao.NoticeVillageDao;
import com.quectel.core.module.notice.dto.NoticeDto;
import com.quectel.core.module.notice.entity.NoticeEntity;
import com.quectel.core.module.notice.entity.NoticeVillageEntity;
import com.quectel.core.module.notice.service.NoticeService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-12-13 16:36:57
 */
@DubboService
public class NoticeServiceImpl implements NoticeService {

    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeServiceImpl.class);

    private static final String NOTICE_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "NOTICE_CACHE_BY_ID:";

    @Autowired
    private NoticeDao noticeDao;
    @Autowired
    private VillageService villageService;
    @Autowired
    private NoticeVillageDao noticeVillageDao;


    /**
     * save or update
     * 保存当前对象和相关联的小区
     *
     * @param noticeDto
     */
    @Override
    @Transactional
    public void saveOrUpdate(NoticeDto noticeDto) {
        if (noticeDto.getId() == null) {
            Long id = save(noticeDto);
            noticeDto.setId(id);
        } else {
            updateById(noticeDto);
        }

        //删除关联关系
        noticeVillageDao.delete(new LambdaQueryWrapper<NoticeVillageEntity>()
                .eq(NoticeVillageEntity::getNoticeId, noticeDto.getId()));
        if (NoticeConstants.BINDING_VILLAGE_PART.equals(noticeDto.getIsBindingAll())) {
            //部分关联
            if (CollectionUtils.isNotEmpty(noticeDto.getVillageDtoList())) {
                for (VillageDto villageDto : noticeDto.getVillageDtoList()) {
                    NoticeVillageEntity noticeVillageEntity = new NoticeVillageEntity();
                    noticeVillageEntity.setId(Snowflake.nextId());
                    noticeVillageEntity.setNoticeId(noticeDto.getId());
                    noticeVillageEntity.setVillageId(villageDto.getId());
                    noticeVillageDao.insert(noticeVillageEntity);
                }
            }
        }
    }

    @Override
    public NoticeDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                NOTICE_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public NoticeDto selectById(Long id) {
        NoticeEntity entity = noticeDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, NoticeDto.class));
    }

    @Override
    public List<NoticeDto> queryList(Map<String, Object> params) {
        List<NoticeEntity> list = noticeDao.queryList(params);
        List<NoticeDto> result = CopyUtils.copyList(list, NoticeDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private NoticeDto paddingField(NoticeDto dto) {
        if (dto != null) {
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return noticeDao.queryTotal(params);
    }

    @Override
    public Long save(NoticeDto dto) {
        NoticeEntity entity = CopyUtils.copyObj(dto, NoticeEntity.class);
        entity.setId(Snowflake.nextId());
        noticeDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(NoticeDto dto) {
        NoticeEntity entity = CopyUtils.copyObj(dto, NoticeEntity.class);
        noticeDao.updateById(entity);
        CacheKit.invalidRedisCache(NOTICE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(NoticeDto dto) {
        NoticeEntity entity = CopyUtils.copyObj(dto, NoticeEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, noticeDao);

        CacheKit.invalidRedisCache(NOTICE_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        noticeDao.deleteById(id);
        //删除关联关系
        noticeVillageDao.delete(new LambdaQueryWrapper<NoticeVillageEntity>()
                .eq(NoticeVillageEntity::getNoticeId, id));

        CacheKit.invalidRedisCache(NOTICE_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
